package com.qiantu.phone.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.adapter.ImageAdapter;
import com.qiantu.phone.ui.dialog.PictureSelectDialog;

import java.util.List;

/**
 * time   : 2021/12/22
 */
public final class FeedbackActivity extends AppActivity {

    private static final int PICK_IMAGE_REQUEST_CODE = 666;

    @Log
    public static void start(Context context) {
        Intent intent = new Intent(context, FeedbackActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    private RecyclerView imageRecyclerView;
    private TextView btnSubmit;
    private ImageAdapter imageAdapter;
    private PictureSelectDialog mPictureSelectDialog;
    private XXPermissions xxPermissions;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initView() {
        imageRecyclerView = findViewById(R.id.image_recycler_view);
        btnSubmit = findViewById(R.id.btn_submit);
        imageAdapter = new ImageAdapter(this, null, 5);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        imageAdapter.setOnItemClickListener(new ImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.Adapter adapter, View view, int position) {
                ImageAdapter imageAdapter = (ImageAdapter) adapter;
                if (imageAdapter.getData() == null || position == imageAdapter.getData().size()) {
                    //添加按钮
                    showPictureSelectDialog();
                } else {
                    //查看图片
//                    ImageViewInfoPagerActivity.start(FeedbackActivity.this, imageAdapter.getData(), position);
                }
            }
        });
        imageRecyclerView.setAdapter(imageAdapter);
        setOnClickListener(R.id.btn_submit);
    }

    @Override
    protected void initData() {

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_submit:

                break;
        }
    }

    /**
     * 选择图片
     */
    private void showPictureSelectDialog(){
        if(mPictureSelectDialog == null){
            mPictureSelectDialog = new PictureSelectDialog(this, R.style.ActionSheetDialogStyle);
            mPictureSelectDialog.setOnItemClickListener(new PictureSelectDialog.OnItemClickListener() {
                @Override
                public void onItemClick(int type) {
                    requestPermission(type);
                }
            });
        }
        mPictureSelectDialog.show();
    }

    private void actionProfilePic(String action) {
        Intent intent = new Intent(this, SelectImageActivity.class);
        intent.setAction(SelectImageActivity.ACTION_SQUARE);
        intent.putExtra("ACTION", action);
        startActivityForResult(intent, PICK_IMAGE_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST_CODE && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            if (bundle != null) {
                //在这里获得了剪裁后的Bitmap对象，可以用于上传
                String imagePath = bundle.getString(SelectImageActivity.CROP_IMAGE_PATH);
                imageAdapter.addData(imagePath);
            }
        }
    }

    private void requestPermission(int type) {
        if(xxPermissions == null){
            xxPermissions = XXPermissions.with(this);
        }
        xxPermissions.permission( Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if(all){
                            if (type == PictureSelectDialog.CAMERA) {
                                actionProfilePic(SelectImageActivity.ACTION_CAMERA);
                                mPictureSelectDialog.dismiss();
                            } else if (type == PictureSelectDialog.ALBUM) {
                                actionProfilePic(SelectImageActivity.ACTION_GALLERY);
                                mPictureSelectDialog.dismiss();
                            } else if (type == PictureSelectDialog.CANCEL) {
                                mPictureSelectDialog.dismiss();
                            }
                        }
                    }
                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        ToastUtils.show(R.string.common_permission_fail_1);
                    }

                });
    }
}